package com.mdp.im.core.service;

import com.mdp.core.api.CacheHKVService;
import com.mdp.im.core.vo.SimpleUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ImLoginUtils {

    public static String prefix="im_user_session_";

    public static CacheHKVService redisTemplate;

    @Autowired
    public  void setRedisTemplate(CacheHKVService redisTemplate) {
        ImLoginUtils.redisTemplate = redisTemplate;
    }

    /**
     * 通过用户编号获取用户的登录信息
     * @param userid
     * @return
     */
    public static SimpleUser getSimpleUser(String userid){
        return (SimpleUser) redisTemplate.get(prefix+userid,userid);
    }

    public static void clearSimpleUser(String userid){
        redisTemplate.delete(prefix+userid,userid);
    }

    public static void putSimpleUser(SimpleUser user){
        String key=prefix+user.getUserid();
        redisTemplate.put(key,user.getUserid(),user);
    }

    public static boolean isOnline(String userid){
        SimpleUser user=getSimpleUser(userid);
        if(user==null){
            return false;
        }else{
            Map<String,Object> channelIds=user.getChannelIds();
            if(channelIds==null){
                return false;
            }else{
                return true;
            }
        }
    }

    /**
     * 某个客户端下线
     * @param userid
     * @param channelId
     */
    public static void removeChannelId(String userid,String channelId){
        SimpleUser userRedis  = getSimpleUser(userid);
        if(userRedis==null){
            return;
        }
        Map<String,Object> channelIds= userRedis.getChannelIds();
        if(channelIds!=null){
            if(channelIds.containsKey(channelId)){
                channelIds.remove(channelId);
                userRedis.setChannelIds(channelIds);
                putSimpleUser(userRedis);
            }

        }
    }

    /**
     * 某个客户端上线
     * @param userid
     * @param channelId
     */
    public static void putChannelId(String userid,String channelId,String ip){
        SimpleUser userRedis  = getSimpleUser(userid);
        Map<String,Object> channelIds=new HashMap<>();
        if(userRedis==null){
            userRedis=new SimpleUser();
            userRedis.setUserid(userid);
            channelIds.put(channelId,ip);
            userRedis.setChannelIds(channelIds);
            putSimpleUser(userRedis);
        }else{//userRedis!=null
            channelIds= userRedis.getChannelIds();
            if(channelIds!=null){
                if(!channelIds.containsKey(channelId)){
                    channelIds.put(channelId,ip);
                    userRedis.setChannelIds(channelIds);
                    putSimpleUser(userRedis);
                }
            }else{//channelIds==null
                channelIds=new HashMap<>();
                channelIds.put(channelId,ip);
                userRedis.setChannelIds(channelIds);
                putSimpleUser(userRedis);
            }
        }


    }
}
